<template>
  <div class="login">
    <img class="left-img" src="../assets/images/login-background.png" alt />
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">
        欢迎使用基础信息
        <br />服务平台！
      </h3>
      <el-form-item prop="username" label="账号">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon
            slot="suffix"
            icon-class="user"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon
            slot="suffix"
            icon-class="password"
            class="el-input__icon input-icon"
          />
        </el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          class="login-btn"
          :loading="loading"
          size="medium"
          type="primary"
          style="width: 100%"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right" v-if="register">
          <router-link class="link-type" :to="'/register'"
            >立即注册</router-link
          >
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Cookies from 'js-cookie'
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: '',
        // password: '123456Aa',
		password: '',
        rememberMe: false,
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '请输入您的账号' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' }
        ]
      },
      loading: false,
      // 注册开关
      register: false,
      redirect: undefined
    }
  },
  computed: {
    roles() {
      return this.$store.getters.roles
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCookie()
  },
  methods: {
    getCookie() {
      const username = Cookies.get('username')
      const password = Cookies.get('password')
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password:
          password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set('username', this.loginForm.username, { expires: 30 })
            Cookies.set('password', encrypt(this.loginForm.password), {
              expires: 30
            })
            Cookies.set('rememberMe', this.loginForm.rememberMe, {
              expires: 30
            })
          } else {
            Cookies.remove('username')
            Cookies.remove('password')
            Cookies.remove('rememberMe')
          }
          this.$store
            .dispatch('Login', this.loginForm)
            .then(() => {
              this.$store.dispatch('dict/getDict')
              this.$store.dispatch('GetInfo').then((userInfo) => {
                this.$store.dispatch('GenerateRoutes').then((accessRoutes) => {
                  let path = '/index'
                  if (this.roles[0].authority === 'ROLE_OPERATE') {
                    path = '/enterprise/index'
                  }
                  this.$router.push({ path }).catch(() => {})
                })
              })
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  height: 100%;
  width: 100%;
  background: rgba(00,00,00,0.05);
  .left-img {
    height: 100%;
    width: 15.45rem;
    margin-right: 1.2rem;
  }
}
.title {
  margin: 0px auto 1.5rem auto;
  font-family: DingTalk JinBuTi, DingTalk JinBuTi;
  font-size: 0.53rem;
  color: #000822;
  line-height: 0.61rem;
}

.login-form {
  width: 6.15rem;
  .el-form-item__label {
    font-size: 0.23rem;
    color: #000822;
    &:before {
      color: #000822 !important;
    }
  }
  ::v-deep {
    .el-input__inner {
      height: 0.82rem;
      font-size: 0.26rem;
      background: #FFFFFF;
      box-shadow: 0px 6 1.2rem .6rem rgba(0, 0, 0, 0.05);
      border-radius: .07rem;
      border: none;
    }
    .el-input__suffix {
      display: flex;
      align-items: center;
    }
  }
  .input-icon {
    margin-right: 0.2rem;
  }
  .login-btn {
    height: 0.75rem;
    background: #1c4088;
    font-size: 0.3rem;
  }
}
</style>
